﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityAndDomainLibrary.DomainModel;
using Log4NetLibrary;

namespace MeterServiceClientApp.UC.Config
{
    public partial class ucMeterPtConfig : UserControl
    {
        #region Private Variables
        ILogService logService;
        DataBaseAccess dbAcess = new DataBaseAccess();

        IList<Meterpoint> _listMeterPts;
        #endregion 

        public ucMeterPtConfig()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(ucMeterPtConfig));

                InitializeComponent();
                LoadData();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        #region Events
        private void dgMeter_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int rowindex = e.RowIndex;
                txtMeterId.Text = _listMeterPts[rowindex].meterpointid.ToString();

                txtMeterName.Text = _listMeterPts[rowindex].name;
                txtLocation.Text = _listMeterPts[rowindex].location;

                txtMeterId.Enabled = false;
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                txtLocation.Text = "";
                txtMeterId.Text = "";

                txtMeterName.Text = "";
                txtMeterName.Enabled = false;
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Meterpoint mp = new Meterpoint();
                mp.meterpointid = Convert.ToInt32(txtMeterId.Text);

                mp.name = txtMeterName.Text;
                mp.location = txtLocation.Text;

                dbAcess.SaveMeterPt(mp);
                _listMeterPts = dbAcess.GetAllMeterpoint();

                dgMeter.DataSource = _listMeterPts;
                lblMessage.Text = "Succesfully Saved";
            }

            catch (Exception ex)
            {
                lblMessage.Text = "Save Failed";
                logService.Error(ex.Message);
                //lblMessage.ForeColor=SystemColors.;
            }
        } 
        #endregion

        #region Helpers
        private void LoadData()
        {
            try
            {
                dgMeter.AutoGenerateColumns = false;
                _listMeterPts = dbAcess.GetAllMeterpoint();

                dgMeter.DataSource = _listMeterPts;
                dgMeter.Refresh();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        } 
        #endregion
    }
}
